package cn.sweet.config;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 文件配置存储类
 *
 * @author wellhor Zhao
 * @version 1.0
 * @date 2021/11/24 4:43 下午
 **/
@State(
        name = "org.intellij.sdk.settings.AppSettingsState",
        storages = @Storage("SweetSettingsPlugin.xml")
)
public class AppSettingState implements PersistentStateComponent<AppSettingState> {

    private String lastEditTime = "@LastEditTime";

    private String lastEditPerson = "@LastEditors";

    private String userName = "";

    private String timeFormat = "yyyy-MM-dd HH:mm:ss";

    private Integer maxCheckLineCount = 15;

    private Boolean isAnyChange = Boolean.TRUE;

    private List<String> suffix = new ArrayList<>(){
        {
            add(".js");
            add(".vue");
            add(".java");
        }
    };

    public static AppSettingState getInstance() {
        return ApplicationManager.getApplication().getService(AppSettingState.class);
    }

    @Override
    public @Nullable AppSettingState getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull AppSettingState state) {
        this.setLastEditPerson(state.getLastEditPerson());
        this.setLastEditTime(state.getLastEditTime());
        this.setUserName(state.getUserName());
        this.setTimeFormat(state.getTimeFormat());
        this.setSuffix(state.getSuffix());
        this.setMaxCheckLineCount(state.getMaxCheckLineCount());
        this.setIsAnyChange(state.getIsAnyChange());
    }

    public String getLastEditTime() {
        return lastEditTime;
    }

    public String getLastEditPerson() {
        return lastEditPerson;
    }

    public String getUserName() {
        return userName;
    }

    public String getTimeFormat() {
        return timeFormat;
    }

    public void setLastEditTime(String lastEditTime) {
        this.lastEditTime = lastEditTime;
    }

    public void setLastEditPerson(String lastEditPerson) {
        this.lastEditPerson = lastEditPerson;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setTimeFormat(String timeFormat) {
        this.timeFormat = timeFormat;
    }

    public String getSuffix() {
        return StringUtils.join(suffix, " ");
    }

    public void setSuffix(String suffix) {
        this.suffix = Arrays.asList(suffix.split(" "));
    }

    public List<String> getSuffixList() {
        return this.suffix;
    }

    public Integer getMaxCheckLineCount() {
        return maxCheckLineCount;
    }

    public void setMaxCheckLineCount(Integer maxCheckLineCount) {
        this.maxCheckLineCount = maxCheckLineCount;
    }

    public Boolean getIsAnyChange() {
        return isAnyChange;
    }

    public void setIsAnyChange(Boolean isAnyChange) {
        this.isAnyChange = isAnyChange;
    }
}
